/*
	Title: 02_Table10.do
	Paper: Brauning, Ivashina, Ozdagli (2025)
	Description: Table 10 - R&D investment and inventory changes in response to covenant trigger 
*/

/*------------------------------------------------------------------------------
								  Table 10
------------------------------------------------------------------------------*/
// Import Table 10 Inventory LHS data ------------------------------------------
use "$data_path/Table10_Inventory.dta", clear 


// Table Setup -----------------------------------------------------------------
putdocx clear 
putdocx begin
putdocx paragraph
putdocx table table10 = (20, 7), border(insideV, nil) border(insideH, nil) border(left, nil) border(right, nil)

* Top margin
putdocx table table10(1,2), colspan(3)
putdocx table table10(1,2) = ("Δ Inventory (% Assets)")
putdocx table table10(1,3), colspan(3)
putdocx table table10(1,3) = ("R&D expenditures (% Assets)")

putdocx table table10(2,4) = ("Both types")
putdocx table table10(2,7) = ("Both types")
putdocx table table10(2,.), border(bottom)

putdocx table table10(3,2) = ("(1)")
putdocx table table10(3,3) = ("(2)")
putdocx table table10(3,4) = ("(3)")
putdocx table table10(3,5) = ("(4)")
putdocx table table10(3,6) = ("(5)")
putdocx table table10(3,7) = ("(6)")
putdocx table table10(3,.), border(bottom)

* Left margin
putdocx table table10(5,1) = ("Bind")
putdocx table table10(7,1) = ("Incurrence bind")
putdocx table table10(9,1) = ("Maintenance bind")
putdocx table table10(12,1) = ("Observations")
putdocx table table10(13,1) = ("R-squared")
putdocx table table10(14,1) = ("Firm FE")
putdocx table table10(15,1) = ("Quarter FE")
putdocx table table10(16,1) = ("Controls")
putdocx table table10(17,1) = ("Controls*Cov-type")
putdocx table table10(18,1) = ("Quarter FE*Cov-type")
putdocx table table10(19,1) = ("H0: Incurrence=Maintenance")
putdocx table table10(20,1) = ("p-value")


// Define Regression Specifications --------------------------------------------
local LHS "D_inv_atq"

local controls0   "L_macroq_tr L_logatq_tr cashflowq_tr" 
local dd_controls " L_DD_Total_final_tr L_DD_Total_final_sq_tr"
local controls    "`controls0' `dd_controls'"

local cond0 "L.dealscan <= 1"
local cond1 "_b1_diff_std <= 2 & _b2_diff_std <= 2"
local cond2 "(!mi(lev_limit) | !mi(intcov_limit) | !mi(Ilev_limit) | !mi(Iintcov_limit))"
local conditions "`cond0' & `cond1' & `cond2'"


// Column (1) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' c.(L_in_covenant_violation) `controls' L_incurrence L_maintenance if `conditions', ///
			 absorb(xdim qdate) cluster(xdim qdate) 

// Store results 
matrix reg_results = r(table)
local violation_b : display %04.2f reg_results["b", "L_in_covenant_violation"]  
local violation_se : display %04.2f reg_results["se", "L_in_covenant_violation"]
local violation_p = reg_results["pvalue", "L_in_covenant_violation"]
if (`violation_p' > 0.1) {
	local violation_sigstars = ""
} 
if (`violation_p' < 0.1) {
	local violation_sigstars = "*"
}
if (`violation_p' < 0.05) {
	local violation_sigstars = "**"
}
if (`violation_p' < 0.01) {
	local violation_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

// Put results into table in Word Document 
putdocx table table10(5,2) = ("`violation_b'`violation_sigstars'")
putdocx table table10(6,2) = ("(`violation_se')")
putdocx table table10(12,2) = (`N')
putdocx table table10(13,2) = (`r2')
putdocx table table10(14,2) = ("Yes")
putdocx table table10(15,2) = ("Yes")
putdocx table table10(16,2) = ("Yes")
putdocx table table10(17,2) = ("No")
putdocx table table10(18,2) = ("No")


// Column (2) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions', ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate) 

// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]  
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p: display %9.4f r(p)

// Put results into table in Word Document
putdocx table table10(7,3) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table10(8,3) = ("(`incurrence_se')")
putdocx table table10(9,3) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table10(10,3) = ("(`maintenance_se')")
putdocx table table10(12,3) = (`N')
putdocx table table10(13,3) = (`r2')
putdocx table table10(14,3) = ("Yes")
putdocx table table10(15,3) = ("Yes")
putdocx table table10(16,3) = ("Yes")
putdocx table table10(17,3) = ("Yes")
putdocx table table10(18,3) = ("Yes")
putdocx table table10(19,3) = (`estimate')
putdocx table table10(20,3) = (`p')

	
// Column (3) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions' & incurrence == 1 & maintenance == 1, ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate) 

// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p: display %9.4f r(p)

// Put results into table in Word Document
putdocx table table10(7,4) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table10(8,4) = ("(`incurrence_se')")
putdocx table table10(9,4) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table10(10,4) = ("(`maintenance_se')")
putdocx table table10(12,4) = (`N')
putdocx table table10(13,4) = (`r2')
putdocx table table10(14,4) = ("Yes")
putdocx table table10(15,4) = ("Yes")
putdocx table table10(16,4) = ("Yes")
putdocx table table10(17,4) = ("Yes")
putdocx table table10(18,4) = ("Yes")
putdocx table table10(19,4) = (`estimate')
putdocx table table10(20,4) = (`p')


// Import Table 10 Inventory LHS data ------------------------------------------
use "$data_path/Table10_Research.dta", clear 


// Define Regression Specifications --------------------------------------------
local trim_scalar = 2
local i = 1 
local LHS "research_atq"

local controls0   "L_macroq_tr L_logatq_tr cashflowq_tr" 
local dd_controls " L_DD_Total_final_tr L_DD_Total_final_sq_tr"
local controls    "`controls0' `dd_controls'"

local cond0 "L.dealscan <=`i'"
local cond1 "_b1_diff_std <=`trim_scalar' & _b2_diff_std<=`trim_scalar'"
local cond2 "(!mi(lev_limit) | !mi(intcov_limit) | !mi(Ilev_limit) | !mi(Iintcov_limit))"
local conditions "`cond0' & `cond1' & `cond2'"


// Column (4) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' c.(L_in_covenant_violation) `controls' L_incurrence L_maintenance if `conditions', ///
			 absorb(xdim qdate) cluster(xdim qdate) 

// Store results 
matrix reg_results = r(table)
local violation_b : display %04.2f reg_results["b", "L_in_covenant_violation"]   
local violation_se : display %04.2f reg_results["se", "L_in_covenant_violation"]
local violation_p = reg_results["pvalue", "L_in_covenant_violation"]
if (`violation_p' > 0.1) {
	local violation_sigstars = ""
} 
if (`violation_p' < 0.1) {
	local violation_sigstars = "*"
}
if (`violation_p' < 0.05) {
	local violation_sigstars = "**"
}
if (`violation_p' < 0.01) {
	local violation_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

// Put results into table in Word Document
putdocx table table10(5,5) = ("`violation_b'`violation_sigstars'")
putdocx table table10(6,5) = ("(`violation_se')")
putdocx table table10(12,5) = (`N')
putdocx table table10(13,5) = (`r2')
putdocx table table10(14,5) = ("Yes")
putdocx table table10(15,5) = ("Yes")
putdocx table table10(16,5) = ("Yes")
putdocx table table10(17,5) = ("No")
putdocx table table10(18,5) = ("No")

	
// Column (5) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions', ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate) 

// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]   
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p: display %9.4f r(p)

// Put results into table in Word Document
putdocx table table10(7,6) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table10(8,6) = ("(`incurrence_se')")
putdocx table table10(9,6) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table10(10,6) = ("(`maintenance_se')")
putdocx table table10(12,6) = (`N')
putdocx table table10(13,6) = (`r2')
putdocx table table10(14,6) = ("Yes")
putdocx table table10(15,6) = ("Yes")
putdocx table table10(16,6) = ("Yes")
putdocx table table10(17,6) = ("Yes")
putdocx table table10(18,6) = ("Yes")
putdocx table table10(19,6) = (`estimate')
putdocx table table10(20,6) = (`p')
	
	
// Column (6) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions' & incurrence == 1 & maintenance == 1, ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate) 
	
// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]   
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p: display %9.4f r(p)

// Put results into table in Word Document
putdocx table table10(7,7) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table10(8,7) = ("(`incurrence_se')")
putdocx table table10(9,7) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table10(10,7) = ("(`maintenance_se')")
putdocx table table10(12,7) = (`N')
putdocx table table10(13,7) = (`r2')
putdocx table table10(14,7) = ("Yes")
putdocx table table10(15,7) = ("Yes")
putdocx table table10(16,7) = ("Yes")
putdocx table table10(17,7) = ("Yes")
putdocx table table10(18,7) = ("Yes")
putdocx table table10(19,7) = (`estimate')
putdocx table table10(20,7) = (`p')


// Table Setup (end) -----------------------------------------------------------
putdocx table table10(.,2/7), halign(center)
putdocx table table10(.,1), width(3 in)

* Number formatting
putdocx table table10(6,.), nformat(%04.2f)
putdocx table table10(8,.), nformat(%04.2f)
putdocx table table10(10,.), nformat(%04.2f)
putdocx table table10(12,.), nformat(%9.0fc)
putdocx table table10(13,.), nformat(%9.2g)
putdocx table table10(19,.), nformat(%05.3f)
putdocx table table10(20,.), nformat(%9.4g)

* Table formatting
putdocx table table10(.,.), font("Times New Roman", 9)

// Export Table 4
putdocx save "$tables_path/Table10.docx", replace	

	
	